package com.ruoyi.web.controller.common.exception;

import com.ruoyi.system.domain.FuelInfo;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.domain.TenantInfo;
import com.ruoyi.system.service.IFuelInfoService;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.UUID;

/**
 * 计算空调油费的线程
 */
public class FuelCalculateException extends Thread {

    protected String area;
    protected SysFloor sysFloor;
    protected FuelInfo fuelInfo;
    protected TenantInfo tenantInfo;
    protected String et;

    private IFuelInfoService fuelInfoService;


    protected  boolean falg = true;
    public boolean getFlag(){
        return falg;
    }
    public FuelCalculateException(FuelInfo fuelInfo,String et, IFuelInfoService fuelInfoService,SysFloor sysFloor, TenantInfo tenantInfo){
        this.fuelInfo = fuelInfo;
        this.et = et;
        this.fuelInfoService = fuelInfoService;
        this.sysFloor = sysFloor;
        this.tenantInfo = tenantInfo;
    }
    @Override
    public synchronized void start() {
        if (this.isInterrupted()){
            return;
        }else{
            //计算空调油费
            Double area = ((tenantInfo.getTenantArea()==null)?0.00:tenantInfo.getTenantArea().doubleValue()) + ((tenantInfo.getEmptyArea()==null)?0.00:tenantInfo.getEmptyArea().doubleValue());
            //需要读取楼层参数id并获取参数值  空调油费
            FuelInfo f = new FuelInfo();
            Calendar cal = Calendar.getInstance();
            if ("".equals(et)){
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH) + 1;
                f.setEtldt(String.valueOf(year)+String.valueOf(month));
            }else{
                f.setEtldt(et);
            }
            f.setFuelUUID(UUID.randomUUID().toString());
            f.setFloorId(fuelInfo.getFloorId());
            f.setFloorName(fuelInfo.getFloorName());
            f.setTenantId(fuelInfo.getTenantId());
            f.setTenantName(fuelInfo.getTenantName());
            f.setTenantArea(String.valueOf(area));
            f.setNum(fuelInfo.getNum());
            f.setSerial_number(fuelInfo.getSerial_number());
            //判断租户是否设置了油费参数
            if(tenantInfo.getCharges().contains("6")){
                DecimalFormat df = new DecimalFormat("#.00");
                f.setFuel_amt(df.format(area*Double.parseDouble(sysFloor.getAirConditionOilParam())*tenantInfo.getChargeCoefficient()));
                f.setFuel_total_amt(df.format(area*Double.parseDouble(sysFloor.getAirConditionOilParam())*Double.parseDouble(sysFloor.getOilSum())*tenantInfo.getChargeCoefficient()));//后期调整
            }else{
                f.setFuel_amt("0.00");
                f.setFuel_total_amt("0.00");//后期调整
            }
           falg=fuelInfoService.insertFuelInfo(f)>0?true:false;
        }
    }

}
